home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 494 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.4 KB

  1. Path: chronicle.mti.sgi.com!austern
  2. From: "brian (b.c.) white" <bcwhite@bnr.ca>
  3. Newsgroups: comp.std.c++
  4. Subject: Q: Generic Callbacks -- "Object->*func(...)"
  5. Date: 14 Feb 1996 15:28:08 PST
  6. Organization: Bell-Northern Research Ltd.
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4fti32$p3p@bcarh8ab.bnr.ca>
  9. NNTP-Posting-Host: isolde.mti.sgi.com
  10. X-Original-Date:  Wed, 14 Feb 1996 20:52:50 +0000 
  11. Content-Identifier:  Q: Generic Ca... 
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBVAwUBMSJwGEy4NqrwXLNJAQFCGQIAjocH85wmXe4iPy5dPOW9Bv5DmnV1f/Zg
  14.     eEEgJe80IPbKIVc1H7vnij8c4Utxz1qX3OLnGbYeUJf9aQSwz3dAuA==
  15.     =avqK
  16. Originator: austern@isolde.mti.sgi.com
  17.  
  18. Does the C++ standard allow for a generic callback to be specified?
  19.  
  20. Basically, I'd like to be able to pass an arbitrary object and function of
  21. that object to be called at some later time.  For example:
  22.  
  23. void DoIt(ANYOBJECT* Object, void (ANYOBJECT::*CallBack)( ...parmlist... ))
  24. {
  25.     [...]
  26.     Object->*CallBack( ...parms... );
  27.     [...]
  28. }
  29.  
  30.  
  31. I'd like to be able to call "DoIt" with any type of object and a pointer to
  32. any of that object's member functions that match the 'parmlist'.  As it
  33. stands, any object passed in would have to be in the same class hierarchy
  34. as ANYOBJECT, thus basically requiring a unified class hierachy in order to
  35. do this genericly.
  36.  
  37. Here is a more complex example that actually stores the object and member
  38. function pointer so it can be called at a later time:
  39.  
  40. typedef    void (ANYOBJECT::*CallBackFunc)( ...parmlist... );
  41. ANYOBJECT*    MyClass::CallObj;
  42. CallBackFunc    MyClass::CallFunc;
  43.  
  44. void MyClass::Set(ANYOBJECT* Object, CallBackFunc* Func)
  45. {
  46.     CallObj = Object;
  47.     CallFunc= Func;
  48. }
  49.  
  50. void MyClass::Call( ...parms... )
  51. {
  52.     CallObj->*Func( ...parms... );
  53. }
  54.  
  55.  
  56. Templates do not work well because the type of object to be stored could
  57. be based on the internal state of the program and not know until run-time,
  58. long after the actual instance of "MyClass" has been created.
  59.  
  60. So...  Is this possible?
  61.  
  62.  
  63.                                         Brian
  64.                                  ( bcwhite@bnr.ca )
  65.  
  66. -------------------------------------------------------------------------------
  67.     In theory, theory and practice are the same.  In practice, they're not.
  68. ---
  69. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  70.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy is
  71.   in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
  72.